<?php
namespace Home\Logic;

class PayLogic {
	
	/**
	 * 支付方式列表
	 * @return multitype:
	 */
	public static function getPayApiList( $cid ) {
		$m = M("ProductPayApi");
		$produtPayApiLists = $m->where("cid=%d and status=1", $cid)->getField("tag,name") or $produtPayApiLists = array(); // 获得商家设置的支付接口
		return $produtPayApiLists;
	}
	
	/**
	 *  获得接口配置
	 * @param unknown $cfg
	 * array(
	 * 	'paymentMethod', // 支付方式(*): Alipay,WxTenpay
	 * )
	 * @return multitype:
	 */
	public static function getApiConfig( $cid, $cfg ) {
		// 支付方式
		$apitype = $cfg['paymentMethod'];
		// 获得支付接口
		$productPayApi = D("ProductPayApi")->where("cid=%d and tag='%s'", $cid, $apitype)->find(); // 获得支付方式配置
		$config = (array)unserialize($productPayApi['cfg']);
		// 附加参数
		$config['wparam'] or $config['wparam'] = array();
		// 不同支付方式下的配置
		if ( 'WxTenpay' == $apitype) { // 微信支付时读取开发者配置
			$wechat = M('Wechat')->where(array('cid'=>$cid))->find();
			$config = array_merge($config, array('appid' => $wechat['appid'], 'appsecret' => $wechat['appsecret'], ));
		}
		return $config;
	}
	
	
	/**
	 * 支付回调接口
	 * @param unknown $apitype
	 * @return Ambigous <string, mixed>
	 */
	public static function getCallBackUrl( $apitype, $cid, $controllerName, $action, $debug = false ) {
		$controllerName or $controllerName = CONTROLLER_NAME;
		$action or $action = "notify";
		return U("Home/{$controllerName}".($debug?'Debug':'')."/{$action}", array('sid' => $cid, 'apitype' => $apitype), false, true);
	}
	
}